package com.t18;

import java.nio.ByteBuffer;

import static com.Print.print;
import static com.Print.printnb;

/**
 * Created by xuyiwei on 2018/9/2.
 */
public class GetData {
    private static final int BSIZE = 1024;
    public static void main(String[] args) {
        ByteBuffer bb = ByteBuffer.allocate(BSIZE);
        // 分配自动零字节:
        int i = 0;
        while(i++ < bb.limit())
            if(bb.get() != 0)
                //因为所有字节都是0  因此输出都是nonzero
                print("nonzero");
        //由limit()觉得 一共检测1024次
        print("i = " + i);
        bb.rewind();
        // 往字节里面存放Howdy
        bb.asCharBuffer().put("Howdy!");
        char c;
        while((c = bb.getChar()) != 0)
            printnb(c + " ");
        print();
        bb.rewind();
        // Store and read a short:
        bb.asShortBuffer().put((short)471111142);
        print(bb.getShort());
        bb.rewind();
        // Store and read an int:
        bb.asIntBuffer().put(99471142);
        print(bb.getInt());
        bb.rewind();
        // Store and read a long:
        bb.asLongBuffer().put(99471142);
        print(bb.getLong());
        bb.rewind();
        // Store and read a float:
        bb.asFloatBuffer().put(99471142);
        print(bb.getFloat());
        bb.rewind();
        // Store and read a double:
        bb.asDoubleBuffer().put(99471142);
        print(bb.getDouble());
        bb.rewind();
        /**
         * i = 1025
         H o w d y !
         12390  99471142   99471142
         9.9471144E7  9.9471142E7
         */
    }
}
